package com.bai.localshop.controller.admin;

import com.bai.localshop.pojo.dto.ProductUpdateDTO;
import com.bai.localshop.exception.BaseException;
import com.bai.localshop.pojo.dto.ProductQueryDTO;
import com.bai.localshop.pojo.dto.ProductSaveDTO;
import com.bai.localshop.pojo.vo.AdminProductPageVO;
import com.bai.localshop.pojo.vo.ProductVO;
import com.bai.localshop.result.Result;
import com.bai.localshop.service.ProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 管理端商品控制器
 */
@RestController("adminProductController")
@RequestMapping("/api/admin/products")
@RequiredArgsConstructor
@Slf4j
public class ProductController {

    private final ProductService productService;

    /**
     * 分页查询商品
     */
    @GetMapping("/page")
    public Result<IPage<AdminProductPageVO>> pageQuery(ProductQueryDTO requestParam) {
        return Result.success(productService.pageQuery(requestParam, AdminProductPageVO.class));
    }

    /**
     * 根据ID查询商品
     */
    @GetMapping("/{id}")
    public Result<ProductVO> getById(@PathVariable Long id) {
        ProductVO product = productService.getById(id);
        if (product == null) {
            throw new BaseException("商品不存在");
        }
        return Result.success(product);
    }

    /**
     * 获取热门商品数据
     */
    @GetMapping("/hot")
    public Result<List<ProductVO>> getHotProduct(){
        List<ProductVO> productVOList =productService.getHotProduct();
        return Result.success(productVOList);
    }

    /**
     * 新增商品
     */
    @PostMapping
    public Result<String> save(@Valid @RequestBody ProductSaveDTO requestParam) {
        productService.save(requestParam);
        return Result.success("新增成功");
    }

    /**
     * 修改商品
     */
    @PutMapping
    public Result<String> update(@Valid @RequestBody ProductUpdateDTO requestParam) {
        productService.update(requestParam);
        return Result.success("修改成功");
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        productService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 更新商品状态
     */
    @PutMapping("/{id}/status")
    public Result<String> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        productService.updateStatus(id, status);
        return Result.success("状态更新成功");
    }
}